package 分类.链表;


import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class 反转链表 {
    public ListNode reverseList(ListNode head) {
        ListNode pre=null;
        ListNode cur=head;
        ListNode temp=null;
        while (cur!=null){
            temp=cur.next;
            cur.next=cur;
            pre=cur;
            cur=temp;
        }
        return pre;
    }

    public static int numComponents(ListNode head, int[] nums) {
        ListNode a=head,b=head;
        Set<Integer> set=new HashSet<>();
        for (int i:nums){
            set.add(i);
        }
        int res=0;

        boolean qian=false;
        while(a!=null){
            if(set.contains(a.val)){
                qian=true;
                set.remove(a.val);
            }else {
                if(qian){
                    res++;
                }
                qian=false;



            }
            a=a.next;
        }

        while(b.next!=null){
            b=b.next;
        }
        for(int i:nums){
            if(i==b.val){
                res++;
            }
        }
        return res;
    }

    public boolean areAlmostEqual(String s1, String s2) {
      boolean res=false;
      int num=0;
      Set<Integer> set=new HashSet<>();
      for (int i=0;i<s1.length();i++){
          if(s1.charAt(i)!=s2.charAt(i)){
              set.add(i);
              num++;
          }
      }
      if(num==2){
         Object[] nums=set.toArray();
         int a=(Integer) nums[0];
         int b=(Integer) nums[1];
         if(s1.charAt(a)==s2.charAt(b)&&s1.charAt(b)==s2.charAt(a)){
             return true;
         }
      }
      return num==0?true:false;
    }

    public static void main(String[] args) {
        ListNode list=new ListNode(0);
        int[] nums={0};
        System.out.println(numComponents(list,nums));
    }
}
